import { defineStore } from "pinia";
import { LocalStorage } from "quasar";

import { postAction } from "src/utils/requestAction";
import { ArrayToTree } from "src/utils/arrayAndTree";
import useCommon from "src/composables/useCommon";

export const useStorageStore = defineStore("storage", {
  state: () => ({
    dcDict: undefined,
    dcSettings: undefined,
    dcBackend: undefined,
    dcFrontend: undefined,
    dcSqlParam: undefined,
    goVersion: undefined,
    ginVersion: undefined,
  }),
  getters: {},
  actions: {
    async SetDcDict() {
      const res = await postAction("public/get-dict-all");
      if (res.code === 1) {
        const dictDetail = res.data.records;
        for (let i of dictDetail) {
          i.value = i.dict_code;
          i.label = i.dict_label;
        }
        const dictList = ArrayToTree(dictDetail, "dict_code", "parent_code");
        let dict = {};
        for (let d of dictList) {
          dict[d.dict_code] = d.children;
        }
        this.dcDict = dict;
        LocalStorage.set("dc-dict", dict);
      }
    },

    async SetDcBackend() {
      const res = await postAction("public/get-config-backend-all");
      if (res.code === 1) {
        const backend = {};
        res.data.records.forEach((item) => {
          backend[item.config_item] = item.item_custom
            ? item.item_custom
            : item.item_default;
        });
        this.dcBackend = backend;
        LocalStorage.set("dc-backend", backend);
      }
    },

    async SetDcFrontend() {
      const res = await postAction("public/get-config-frontend-all");
      if (res.code === 1) {
        const frontend = {};
        res.data.records.forEach((item) => {
          frontend[item.config_item] = item.item_custom
            ? item.item_custom
            : item.item_default;
        });
        this.dcFrontend = frontend;
        LocalStorage.set("dc-frontend", frontend);
      }
    },

    SetDcGoVersion(goVersion) {
      this.goVersion = goVersion;
      LocalStorage.set("dc-goVersion", goVersion);
    },

    SetDcGinVersion(ginVersion) {
      this.ginVersion = ginVersion;
      LocalStorage.set("dc-ginVersion", ginVersion);
    },

    GetDcDict() {
      const dict = LocalStorage.getItem("dc-dict");
      if (this.dcDict) {
        return this.dcDict;
      } else {
        return dict;
      }
    },
    GetDcBackend() {
      const { DcBackendDefault } = useCommon();
      const backend = LocalStorage.getItem("dc-backend");
      if (this.dcBackend) {
        return this.dcBackend;
      } else if (backend) {
        return backend;
      } else {
        return DcBackendDefault;
      }
    },
    GetDcFrontend() {
      const { DcFrontendDefault } = useCommon();
      const frontend = LocalStorage.getItem("dc-frontend");
      if (this.dcFrontend) {
        return this.dcFrontend;
      } else if (frontend) {
        return frontend;
      } else {
        return DcFrontendDefault;
      }
    },
    GetDcGoVersion() {
      const goversion = LocalStorage.getItem("dc-goVersion");
      if (this.goVersion) {
        return this.goVersion;
      } else {
        return goversion;
      }
    },
    GetDcGinVersion() {
      const ginversion = LocalStorage.getItem("dc-ginVersion");
      if (this.ginVersion) {
        return this.ginVersion;
      } else {
        return ginversion;
      }
    },
  },
  persist: {
    enabled: true,
  },
});
